package me.militch.demo;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class FootSelectView extends PopupWindow{
    private final String TAG;
    private Context context;
    private View contentView;
    private Activity showActivity;
    private ListView listView;
    private List<String> datas;
    private LayoutInflater mLinflater;
    private TextView optView;
    private RelativeLayout selectView;
    public FootSelectView(Context context){
        super(context);
        TAG = this.getClass().getName();
        this.context = context;
        mLinflater = LayoutInflater.from(context);
        //初始化设置
        initView();
        prefromSet();
    }

    /**
     * 设置数据
     */
    public void setStrings(List<String> datas){
        this.datas.clear();
        this.datas.addAll(datas);
    }
    public void setStrings(String[] datas){
        this.datas.clear();
        this.datas.add("");
        this.datas.add("");
        Collections.addAll(this.datas,datas);
        this.datas.add("");
        this.datas.add("");
    }

    /**
     * 初始化View
     */
    private void initView(){
        contentView = LayoutInflater.from(context).inflate(R.layout.layout_popup_foot,null);
        listView = (ListView) contentView.findViewById(R.id.foot_listview);
        selectView = (RelativeLayout) contentView.findViewById(R.id.foot_select);
        optView = (TextView) contentView.findViewById(R.id.foot_opt);
        //根据选择控件高度，重新设置父容器高度
        int optHeight = measureOptHeight();
        ViewGroup.LayoutParams lp = selectView.getLayoutParams();
        lp.height = optHeight*5;//因为要达到选中中间效果所以是单个高度*奇数
        selectView.setLayoutParams(lp);
        datas = new ArrayList<>();
        listView.setAdapter(datasAdapter);
    }

    /**
     * 获取选择控件高度
     * @return
     */
    private int measureOptHeight(){
        int w = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        optView.measure(w, h);
        return optView.getMeasuredHeight();
    }

    /**
     * 预设PopupWindow
     */
    private void prefromSet(){
        setContentView(contentView);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setBackgroundDrawable(new ColorDrawable(0xFFFFFFFF));
        setOutsideTouchable(true);
        setFocusable(true);
    }

    /**
     * 显示弹窗
     * @param activity 目标Activity
     * @param layout 作用layout
     */
    public void show(Activity activity,int layout){
        showActivity = activity;
        showAtLocation(activity.getLayoutInflater().inflate(layout,null), Gravity.BOTTOM,0,0);
        setBackgroundAlpha(0.5f);
    }

    /**
     * 设置背景透明度
     * @param alpha 透明度值
     */
    private void setBackgroundAlpha(float alpha){
        if(showActivity == null){
            return;
        }
        //获取到Window参数，重设透明度
        WindowManager.LayoutParams params = showActivity.getWindow().getAttributes();
        params.alpha = alpha;
        showActivity.getWindow().setAttributes(params);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        //关闭时恢复透明度
        setBackgroundAlpha(1.0f);
    }

    /**
     * 内部ListView数据适配器
     */
    private BaseAdapter datasAdapter = new BaseAdapter() {

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int position) {
            return datas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if(convertView == null){
                convertView =  mLinflater.inflate(R.layout.item_foot_text,null);
                viewHolder = new ViewHolder();
                viewHolder.textView = (TextView) convertView.findViewById(R.id.item_foot_text);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.textView.setText(datas.get(position));
            return convertView;
        }

        /**
         * 内部View缓存
         */
        class ViewHolder{
            TextView textView;
        }

    };

}
